<template>
  <div class="wrapper">

    <JTable :table-list="tableData" :config="tableConfig">
      <template slot="Actions" slot-scope="{row}">
        <el-button v-if="$_permisson('/core/client/save')" type="text" @click="handleActive(row,'more')">详情</el-button>
        <el-button v-if="$_permisson('/core/client/save')" type="text" @click="handleActive(row,'edit')">修改</el-button>
        <el-button v-if="$_permisson('/core/client/delete')" type="text" @click="handleActive(row,'del')">删除</el-button>
      </template>
    </JTable>

    <el-dialog :title="dialog.title[dialog.active]" :visible.sync="dialog.dialogVisible" destroy-on-close>
      <ClientUpdate :client="selectedUserItem" :read="dialog.active=='more'" @handleBoxEvent="handleDialog" />
    </el-dialog>

    <el-drawer
      class="hiddenOutline"
      title="搜索"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleCloseDrawer"
    >
      <div>搜索</div>
      <!-- <findBox @handleBoxEvent="handleFilter" /> -->
    </el-drawer>
  </div>
</template>

<script>
import JTable from '@/components/JTable'

import pageBtn from '@/views/mixin/pageBtn'
import { getList, delClient, updataClient } from '@/api/client'
import ClientUpdate from './widget/ClientUpdate'
export default {
  components: {
    ClientUpdate,
    JTable
  },
  mixins: [pageBtn],
  props: {},
  data() {
    const listQuery = {
      total: 0,
      page: 1,
      size: 10,
      handleFilter: this.getList
    }
    const dialog = {
      dialogVisible: false,
      title: { created: '新增数据', edit: '修改数据', more: '查看数据' },
      active: ''
    }
    return {
      listLoading: false,
      dialog,
      listQuery,
      currentId: null,
      list: [],
      drawer: false,
      tableConfig: {
        listLoading: this.listLoading,
        pagination: listQuery,
        thead: [
          {
            type: 'selection'
          },
          {
            type: 'index',
            label: '-',
            start: 1
          },
          {
            prop: 'name',
            label: '名称'
          },
          {
            prop: 'clientCode',
            label: '编码'
          },
          {
            prop: 'clientSecret',
            label: '密钥'
          },
          {
            prop: 'status',
            label: '状态'
          },
          {
            prop: 'note',
            label: '备注'
          },
          {
            prop: 'refreshTokenValidity',
            label: 'Rtoken时长'
          },
          {
            prop: 'accessTokenValidity',
            label: 'token时长'
          },
          {
            prop: 'sortNum',
            label: '排序'
          },
          {
            prop: '',
            label: 'Actions',
            type: 'actions',
            width: '230',
            align: 'center'
          }
        ],
        search: ['name', 'clientCode'],
        activeBox: {
          // defaultBtn: true,
          btnRules: { created: '/core/client/save', rulesFuntion: this.$_permisson }, // 按btn个数重左向右
          bindDefaultEvent: this.handleCreated
        }
      }
    }
  },
  computed: {
    tableData() {
      const data = this.list
      const result = data
      return result
    },
    selectedUserItem() {
      const { currentId } = this
      if (currentId === null) return {}
      const result = this.list.find(i => i.id === currentId)
      return result || {}
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  mounted() { },
  methods: {
    showFindBox() {
      this.drawer = true
    },
    handleCloseDrawer() {
      this.drawer = false
    },
    getList(args = {}) {
      const { page: pageNum = 1, size: pageSize = 10, ...param } = args
      const params = { pageNum, pageSize, ...param }
      getList(params).then(res => {
        const { data, code } = res
        if (code === 200) {
          const { list, total } = data
          this.listQuery.total = total
          this.list = list
        }
      })
    },
    delItem(ids) {
      this.$confirm('此操作不可逆转, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delClient(ids).then(res => {
          const { code, message } = res
          if (code === 200) {
            this.$message({
              message,
              type: 'success'
            })
            this.getList()
            return
          }
          this.$message.error(message)
        })
      })
    },
    handleFilter(queryParams) {
      const { listQuery } = this
      if (queryParams) {
        const newParams = { ...listQuery, ...queryParams, page: 1 }
        this.listQuery = newParams
      }
    },
    handleActive(row, target) {
      if (['more', 'edit'].includes(target)) { this.showDialog(target) }
      const { id } = row
      this.currentId = id
      if (target === 'del') {
        this.delItem(id)
        return
      }
    },
    handleCreated() {
      this.setCurrentID()
      this.showDialog()
    },
    setCurrentID() {
      this.currentId = null
    },
    showDialog(target = 'created') {
      this.dialog.dialogVisible = true
      this.dialog.active = target
    },
    closeDialog() {
      this.setCurrentID()
      this.dialog.active = ''
      this.dialog.dialogVisible = false
    },
    handleDialog(emitObj) {
      const { target } = emitObj
      if (!target) {
        return this.closeDialog()
      }
      if (this.currentId) {
        target.id = this.currentId
      }
      this.saveClient(target)
    },
    saveClient(clientItem) {
      updataClient(clientItem).then(res => {
        const { code, message } = res
        if (code === 200) {
          this.$message({
            message,
            type: 'success'
          })
          this.closeDialog()
          this.getList()
          return
        }
        this.$message.error(message)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.wrapper{
   padding: 0px;
   margin: 0px;
}
</style>
